#include <vector>
#include <stack>

constexpr int N = 1E5 + 5;

std::vector<int> adj[N];

int dfn[N], low[N];
std::stack<int> stk;
int bel[N], cnt, cur;

void tarjan(int x) {
    dfn[x] = low[x] = ++cur;
    stk.push(x);

    for (int v : adj[x]) {
        if (!dfn[v]) {
            tarjan(v);
            low[x] = std::min(low[x], low[v]);
        } else if (bel[v] == 0) {
            low[x] = std::min(low[x], dfn[v]);
        }
    }

    if (dfn[x] == low[x]) {
        int y;
        ++cnt;
        do {
            y = stk.top();
            stk.pop();
            bel[y] = cnt;
        } while (x != y);
    }
}